<template>
  <el-container class="home">
      <el-aside width="220px">
        <el-menu :default-active="activePath" background-color="#545c64" text-color="#fff" active-text-color="#409EFF"
          uniqueOpened="true" router>
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-user-solid"></i>
              <span>账号管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="userList" @click="saveNavPath('userList')"><i class="el-icon-tickets"></i>账号列表</el-menu-item>
              <el-menu-item index="userOperate" @click="saveNavPath('userOperate')" :disabled="isTrue"><i class="el-icon-circle-plus"></i>账号添加</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-picture"></i>
              <span>轮播图管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="pictureList" @click="saveNavPath('pictureList')"><i class="el-icon-tickets"></i>轮播图列表</el-menu-item>
              <el-menu-item index="pictureOperate" @click="saveNavPath('pictureOperate')" :disabled="isTrue"><i class="el-icon-zoom-in"></i>轮播图添加</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>类目管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="typeList" @click="saveNavPath('typeList')"><i class="el-icon-tickets"></i>类目列表</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="4">
            <template slot="title">
              <i class="el-icon-s-grid"></i>
              <span>菜品管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="foodList" @click="saveNavPath('foodList')"><i class="el-icon-tickets"></i>菜品列表</el-menu-item>
              <el-menu-item index="foodOperate" @click="saveNavPath('foodOperate')" :disabled="isTrue"><i class="el-icon-plus"></i>菜品添加</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="5">
            <template slot="title">
              <i class="el-icon-s-order"></i>
              <span>订单管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="orderList" @click="saveNavPath('orderList')"><i class="el-icon-tickets"></i>订单列表</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-menu-item index="datamanagement" @click="saveNavPath('datamanagement')">
            <i class="el-icon-s-data"></i>
            <span slot="title">数据管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container class="right">
        <el-header>
          <span>后台管理系统</span>
          <el-button type="primary" @click="logout">logout</el-button>
        </el-header>
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
  </el-container>
</template>

<script>
  import jwtDecode from 'jwt-decode'
  export default {
    data() {
      return {
        isTrue: false,
        // 点击侧边栏记录的激活区块
        activePath: ''
      }
    },
    beforeCreate() {
      this.$goEasy.subscribe({
        channel: 'my_channel',
        onMessage: (message) => {
          // console.log('Channel:' + message.channel + 'content:' + message.content);
          this.newOrder(message.content)
        }
      })
      this.$goEasy.subscribe({
        channel: 'my_ch',
        onMessage: (message) => {
          // console.log('Channel:' + message.channel + 'content:' + message.content);
          this.reminder(message.content)
        }
      })
    },
    created() {
      this.activePath = window.sessionStorage.getItem('activePath')
      const tokenStr = window.sessionStorage.getItem('token')
      const decode = jwtDecode(tokenStr)
      if (decode.scope !== '超级管理员') {
        this.isTrue = true
      }
    },
    methods: {
      // 催单
      reminder(content) {
        this.$message({
          message: content,
          type: 'warning',
          duration: 0,
          showClose: true
        })
      },
      // 有新订单到
      newOrder(content) {
        this.$message({
          message: content,
          type: 'success',
          duration: 0,
          showClose: true
        })
      },
      logout() {
        window.sessionStorage.clear('activePath')
        this.$router.push('/login')
      },
      saveNavPath(activePath) {
        window.sessionStorage.setItem('activePath', activePath)
        window.localstorage.setItem('activePath', activePath)
        this.activePath = window.sessionStorage.getItem('activePath')
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-button {
    position: absolute;
    top: 50%;
    right: 5%;
    transform: translateY(-50%);
  }

  html,
  body {
    height: 100%;
    background-color: #fff;
  }

  .home {
    width: 100%;
    height: calc(100vh);
  }

  .el-header {
    background-color: #FFFFFF;
    color: #333;
    text-align: center;
    line-height: 60px;
    position: relative;
  }

  .el-aside {
    background-color: #545c64;
    color: #333;
    text-align: left;
    max-width: 220px;
    min-width: 220px;
  }

  .right{
    max-width: 1700px;
  }
  .el-main {
    background-color: #F0F2F5;
    color: #333;
    // text-align: center;
    line-height: 100%;

    .main_container {
      height: 100%;
      background-color: #fff;
    }
  }
</style>
